package com.movitech.mobile.cz.base.sys.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.common.util.ListObjCopyUtil;
import com.movitech.mobile.cz.base.common.util.StringUtils;
import com.movitech.mobile.cz.base.modules.controller.RestReturnResult;
import com.movitech.mobile.cz.base.sys.entity.SysArea;
import com.movitech.mobile.cz.base.sys.entity.ext.SysAreaResponseVO;
import com.movitech.mobile.cz.base.sys.mapper.SysAreaMapper;
import com.movitech.mobile.cz.base.sys.mapper.ext.ExtSysAreaMapper;
import com.movitech.mobile.cz.base.sys.service.SRegionalCompanyAreaService;
import com.movitech.mobile.cz.base.sys.service.SysAreaService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 省市区 服务实现类
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-04-10
 */
@Service
public class SysAreaServiceImpl extends ServiceImpl<SysAreaMapper, SysArea> implements SysAreaService {

    @Autowired
    private ExtSysAreaMapper extSysAreaMapper;

    @Autowired
    private SRegionalCompanyAreaService sRegionalCompanyAreaService;

    public RestReturnResult allList() {
        RestReturnResult restReturnResult=RestReturnResult.error();
        try {
            EntityWrapper wrapper=new EntityWrapper();
            SysArea sysArea=new SysArea();
            sysArea.setDelFlag(CommonConstants.DEL_FLAG_NORMAL);
            wrapper.setEntity(sysArea);
            wrapper.orderBy(" area_sort ",true);
            List<SysArea> ls=this.selectList(wrapper);
            List<SysAreaResponseVO> responseVOList= ListObjCopyUtil.copyTo(ls,SysAreaResponseVO.class);
            for(SysAreaResponseVO obj:responseVOList){
                obj.setCompanyIds(sRegionalCompanyAreaService.getProvinceAreas(obj.getId()));
            }
            restReturnResult=RestReturnResult.success();
            restReturnResult.setData(responseVOList);
        }catch (Exception e){
            e.printStackTrace();
            restReturnResult=RestReturnResult.error();
        }
        return restReturnResult;
    }

    @Override
    public String getAreaNames(String[] idArr) {
        return extSysAreaMapper.getAreaNames(idArr);
    }
	
	/**
     * 根据ids查询areaNames
     */
	@Override
	public String getSysAreaNamesByIds(String areaIds) {
		if(StringUtils.isBlank(areaIds)){
            return null;
        }
        StringBuffer sb = new StringBuffer();
        List<String> idList = new ArrayList<>();
        for(String id : areaIds.split(",")){
            idList.add(id);
        }
        List<SysArea> sysAreaList = this.selectBatchIds(idList);
        if(sysAreaList != null && sysAreaList.size() > 0){
            for(int i = 0;i < sysAreaList.size();i++){
                if(i == 0){
                    sb.append(sysAreaList.get(i).getAreaName());
                }else{
                    sb.append(","+sysAreaList.get(i).getAreaName());
                }
            }
        }
        return sb.toString();
	}

    /**
     * 根据ids查询areaCodes
     */
    @Override
    public String getSysAreaCodesByIds(String areaIds) {
        if(StringUtils.isBlank(areaIds)){
            return null;
        }
        StringBuffer sb = new StringBuffer();
        List<String> idList = new ArrayList<>();
        for(String id : areaIds.split(",")){
            idList.add(id);
        }
        List<SysArea> sysAreaList = this.selectBatchIds(idList);
        if(sysAreaList != null && sysAreaList.size() > 0){
            for(int i = 0;i < sysAreaList.size();i++){
                if(i == 0){
                    sb.append(sysAreaList.get(i).getAreaCode());
                }else{
                    sb.append(","+sysAreaList.get(i).getAreaCode());
                }
            }
        }
        return sb.toString();
    }
}
